<?php

namespace app\admin\controller\appForm;

use app\admin\model\AppForm;
use app\admin\model\ApprovalProcess;
use app\admin\model\Picture;
use app\admin\model\User;
use think\Exception;

trait Reviews
{
    /**
     * 我的审核-待审核列表
     * @return mixed
     */
    public function waitReviewList()
    {
        $sealAppFormStatues = $this->configModel->getSealAppFormStatues();
        //$seals = $this->sealModel->scope('company', $this->company->id)->select();
        $condition['is_del'] = array('eq', 0);
        $condition['company_id'] = $this->company->id;
        $seals = db('seal')->where($condition)->select();
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $where = [];
        $params = $this->request->param();
        if ($params['sealId']) {
            $where['AppForm.seal_id'] = $params['sealId'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['AppForm.create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        if ($params['name']) {
            $where['AppForm.name'] = ['like', '%' . $params['name'] . '%'];
        }

        $uid = $this->getCurrentUser();
        // $list = $this->model
        //     ->hasWhere('processFlows', ['uid' => $uid, 'status' => ApprovalProcess::$AUDIT_WAITING])
        //     ->with($this->with ? $this->with : null)
        //     ->where($where)
        //     ->where('AppForm.company_id', $this->company->id)
        //     ->order($sort, $order)
        //     ->paginate($limit);
        //print_r(session('is_admin'));die;
        if (session('is_admin') == 3) {
            $list = $this->model
                ->hasWhere('processFlows', ['uid' => $uid, 'status' => ApprovalProcess::$AUDIT_WAITING])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->paginate($limit);
        } elseif (session('is_admin') == 2) {
            //企业管理员
            //print_r(ApprovalProcess::$AUDIT_WAITING);die;
            $list = $this->model
                ->hasWhere('processFlows', ['status' => ApprovalProcess::$AUDIT_WAITING])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->paginate($limit);
        } else {
            $list = $this->model
                ->hasWhere('processFlows', ['status' => ApprovalProcess::$AUDIT_WAITING])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->paginate($limit);
        }
        $this->assign('title', 'Reviews');
        $this->assign('nav', 'appForm');
        $this->assign('subnav', 'reviews');
        $this->assign('params', $params);
        $this->assign('seals', $seals);
        $this->assign('statuses', $sealAppFormStatues);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function dc2()
    {
        // $company = $this->company;
        // $company_id=$company->id;
        // $where['company_id'] = $company_id;

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = $this->model
            ->hasWhere('processFlows', ['status' => ApprovalProcess::$AUDIT_WAITING])
            ->with($this->with ? $this->with : null)
            ->where($where)
            ->where('AppForm.company_id', $this->company->id)
            ->order($sort, $order)
            ->paginate($limit);

        vendor('phpexcel/PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //exit($objPHPExcel);
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '印章名称')
            ->setCellValue('B1', '文件编号')
            ->setCellValue('C1', '审批类型')
            ->setCellValue('D1', '文件名称')
            ->setCellValue('E1', '申请用章次数')
            ->setCellValue('F1', '状态')
            ->setCellValue('G1', '备注')
            ->setCellValue('H1', '用章时间')
            ->setCellValue('I1', '申请时间');

        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);


        for ($i = 0; $i < count($list); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['seal']['name']);                                                      //印章名称
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['seal']['number']);                                                    //文件编号
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['approval']['type_name']);                                             //审批类型
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['name']);                                                              //文件名称
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['frequency']);                                                         //申请用章次数
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['status_txt']);                                                        //状态
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['remark']);                                                            //备注
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['chapter_time'] ? date('Y-m-d H:i:s', $list[$i]['chapter_time']) : '');//用章时间
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['create_time']);                                                       //申请时间
        }
        //7.设置保存的Excel表格名称
        $filename = 'yz' . date('ymd', time()) . '.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('cs');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $filename . '"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;


    }

    /**
     * 审核
     * <p>如果传入accept 则为审核通过，如果为reject 则为拒绝</p>
     * @param $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function waitReviewAudit($id)
    {
        $params = $this->request->param();
        $currentUser = $this->getCurrentUser();
        $user = User::find($currentUser);
        $row = $this->model->with(['processFlows.user', 'user', 'seal', 'approval'])->find($id);
        if (!$row) {
            $this->error('没有找到相关申请表');
        }
        if ($this->request->isPost()) {

            try {
                if ($this->request->ext() == 'json') {
                    switch ($params['status']) {
                        case 'reject':
                            $row->reject($user, $params['describe']);
                            break;
                        case 'accept':
                            $row->audit();
                    }
                    $this->success('审核成功');
                }
            } catch (Exception $e) {
                $this->error($e->getMessage());
            }
        }


        //当前审核人
        $condition['form_id'] = $id;
        $condition['status'] = array('eq', 1);
        $reviewer = db('approval_process')->where($condition)->order('id desc')->value('uid');
        $uid = $this->getCurrentUser();

        if ($reviewer == $uid) {
            //print_r($uid);die;
            $this->assign('uu', 1);
        }


        $value = processToGroupSort($row->processFlows);
        $this->assign("processFlowGroups", $value);
        $this->assign("user", $row->user);
        $this->assign("seal", $row->seal);
        $this->assign("approval", $row->approval);
        $this->assign("attachments", []);
        if ($row->imgs) {
            $value1 = (new Picture())->getByIdIn(explode(",", $row->imgs));
            $this->assign("attachments", $value1);
        }
        if ($row->used_img) {
            $value2 = (new Picture())->getByIdIn(explode(",", $row->used_img));
            $this->assign("attachments2", $value2);
        }
        //print_r($value);die;
        //文档
        if ($row->file) {
            $file1 = $row->file;
            $file = explode(',', $file1);
            foreach ($file as $k => $v) {
                $bb[$k]['src'] = "https://" . $_SERVER['HTTP_HOST'] . db('picture')->where(['id' => $v])->value('path');
                $bb[$k]['name'] = db('picture')->where(['id' => $v])->value('name');
            }
            $wd = $bb;
        } else {
            $wd = '';
        }
        $this->assign("wd", $wd);


        $ll = db('approval_process')->where(['form_id' => $id])->order('id asc')->select();
        //print_r($ll);die;
        foreach ($ll as $k => $v) {
            if ($v['remark']) {
                if ($v['remark'] == 1) {
                    $reason = '文件名称不全，请核实';
                } elseif ($v['remark'] == 2) {
                    $reason = '用章次数可能过多，请重新设置';
                } elseif ($v['remark'] == 3) {
                    $reason = '图片上传不够清晰，请核对后重新上传';
                } elseif ($v['remark'] == 4) {
                    $reason = $v['remark_content'];
                }
            }


        }
        $this->assign("reason", $reason);
        $this->assign("row", $row);
        return $this->fetch();
    }

    /**
     * 我的审核-我的审核列表
     */
    public function reviewList()
    {
        $sealAppFormStatues = $this->configModel->getSealAppFormStatues();
        //$seals = $this->sealModel->scope('company', $this->company->id)->select();
        $condition['is_del'] = array('eq', 0);
        $condition['company_id'] = $this->company->id;
        $seals = db('seal')->where($condition)->select();
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $where = [];
        $params = $this->request->param();
        if ($params['sealId']) {
            $where['AppForm.seal_id'] = $params['sealId'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['AppForm.create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        if ($params['name']) {
            $where['AppForm.name'] = ['like', '%' . $params['name'] . '%'];
        }

        $uid = $this->getCurrentUser();

        if (session('is_admin') == 3) {
            $list = $this->model
                ->hasWhere('processFlows', ['uid' => $uid])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->paginate($limit);
        } elseif (session('is_admin') == 2) {
            //企业管理员
            $where['AppForm.company_id'] = $this->company->id;
            $list = $this->model
                ->hasWhere('processFlows', ['uid' => $uid])
                ->with($this->with ? $this->with : null)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->paginate($limit);
        } else {
            $where['AppForm.company_id'] = $this->company->id;
            $list = $this->model
                ->hasWhere('processFlows')
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
        }
        $this->assign('title', 'Reviews');
        $this->assign('nav', 'appForm');
        $this->assign('subnav', 'reviews');
        $this->assign('params', $params);
        $this->assign('seals', $seals);
        $this->assign('statuses', $sealAppFormStatues);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function dc3()
    {
        $sealAppFormStatues = $this->configModel->getSealAppFormStatues();
        $seals = $this->sealModel->scope('company', $this->company->id)->select();
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $where = [];
        $params = $this->request->param();
        if ($params['sealId']) {
            $where['AppForm.seal_id'] = $params['sealId'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['AppForm.create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        if ($params['name']) {
            $where['AppForm.name'] = ['like', '%' . $params['name'] . '%'];
        }

        $uid = $this->getCurrentUser();

        if (session('is_admin') == 3) {
            $list = $this->model
                ->hasWhere('processFlows', ['uid' => $uid])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->select();
        } elseif (session('is_admin') == 2) {
            //企业管理员
            $where['AppForm.company_id'] = $this->company->id;
            $list = $this->model
                ->hasWhere('processFlows', ['uid' => $uid])
                ->with($this->with ? $this->with : null)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->select();
        } else {
            $where['AppForm.company_id'] = $this->company->id;
            $list = $this->model
                ->hasWhere('processFlows')
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->order($sort, $order)
                ->select();
        }

        vendor('phpexcel/PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //exit($objPHPExcel);
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '印章名称')
            ->setCellValue('B1', '文件编号')
            ->setCellValue('C1', '审批类型')
            ->setCellValue('D1', '文件名称')
            ->setCellValue('E1', '申请用章次数')
            ->setCellValue('F1', '状态')
            ->setCellValue('G1', '备注')
            ->setCellValue('H1', '用章时间')
            ->setCellValue('I1', '申请时间');


        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);


        for ($i = 0; $i < count($list); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['seal']['name']);                                                      //印章名称
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['seal']['number']);                                                    //文件编号
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['approval']['type_name']);                                             //审批类型
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['name']);                                                              //文件名称
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['frequency']);                                                         //申请用章次数
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['status_txt']);                                                        //状态
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['remark']);                                                            //备注
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['chapter_time'] ? date('Y-m-d H:i:s', $list[$i]['chapter_time']) : '');//用章时间
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['create_time']);                                                       //申请时间
        }
        //7.设置保存的Excel表格名称
        $filename = 'yz' . date('ymd', time()) . '.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('cs');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $filename . '"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;


    }

    /**
     * 我的审核-归档审核
     */
    public function reviewListDetail($id)
    {
        $currentUser = $this->getCurrentUser();
        $row = $this->model
            //->hasWhere('processFlows', ['uid' => $currentUser])
            //->with(['processFlows.user', 'user', 'seal', 'approval'])
            //->where('company_id', $this->company->id)
            ->find($id);
        if (!$row) {
            $this->error('没有找到相关申请表');
        }
        $value = processToGroupSort($row->processFlows);
        $this->assign("processFlowGroups", $value);
        $this->assign("user", $row->user);
        $this->assign("seal", $row->seal);
        $this->assign("approval", $row->approval);
        $this->assign("attachments", []);
        if ($row->imgs) {
            $value1 = (new Picture())->getByIdIn(explode(",", $row->imgs));
            $this->assign("attachments", $value1);
        }
        if ($row->used_img) {
            $value2 = (new Picture())->getByIdIn(explode(",", $row->used_img));
            $this->assign("attachments2", $value2);
        }
        if ($row->used_times > $row->frequency) {
            $row->used_times = $row->frequency;
        }
        if ($row->remaining_times < 0) {
            $row->remaining_times = 0;
        }
        //print_r($value);die;
        //文档
        if ($row->file) {
            $file1 = $row->file;
            $file = explode(',', $file1);
            foreach ($file as $k => $v) {
                $bb[$k]['src'] = "https://" . $_SERVER['HTTP_HOST'] . db('picture')->where(['id' => $v])->value('path');
                $bb[$k]['name'] = db('picture')->where(['id' => $v])->value('name');
            }
            $wd = $bb;
        } else {
            $wd = '';
        }
        $this->assign("wd", $wd);


        $ll = db('approval_process')->where(['form_id' => $id])->order('id asc')->select();
        //print_r($ll);die;
        foreach ($ll as $k => $v) {
            if ($v['remark'] == 1) {
                $reason = '文件名称不全，请核实';
            } elseif ($v['remark'] == 2) {
                $reason = '用章次数可能过多，请重新设置';
            } elseif ($v['remark'] == 3) {
                $reason = '图片上传不够清晰，请核对后重新上传';
            } elseif ($v['remark'] == 4) {
                $reason = $v['remark_content'];
            }

        }
        $this->assign("reason", $reason);
        $this->assign("row", $row);
        return $this->fetch();
    }

    /**
     * 我的审核-归档审核
     */
    public function reviewArchiveList()
    {
        $sealAppFormStatues = $this->configModel->getSealAppFormStatues();
        //$seals = $this->sealModel->scope('company', $this->company->id)->select();
        $condition['is_del'] = array('eq', 0);
        $condition['company_id'] = $this->company->id;
        $seals = db('seal')->where($condition)->select();
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $where = [];
        $params = $this->request->param();
        if ($params['sealId']) {
            $where['AppForm.seal_id'] = $params['sealId'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['AppForm.create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        if ($params['name']) {
            $where['AppForm.name'] = ['like', '%' . $params['name'] . '%'];
        }
        $where['AppForm.status'] = AppForm::$STATUS_FINISHED;

        $uid = $this->getCurrentUser();
        // $list = $this->model
        // ->hasWhere('processFlows', ['uid' => $uid, 'status' => ApprovalProcess::$AUDIT_PASS])
        // ->with($this->with ? $this->with : null)
        // ->where($where)
        // ->where('AppForm.company_id', $this->company->id)
        // ->order($sort, $order)
        // ->paginate($limit);

        if (session('is_admin') == 3) {
            $list = $this->model
                ->hasWhere('processFlows', ['uid' => $uid, 'status' => ApprovalProcess::$AUDIT_PASS])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->paginate($limit);
        } elseif (session('is_admin') == 2) {
            //企业管理员
            $list = $this->model
                ->hasWhere('processFlows', ['status' => ApprovalProcess::$AUDIT_PASS])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->paginate($limit);
        } else {
            $list = $this->model
                ->hasWhere('processFlows', ['status' => ApprovalProcess::$AUDIT_PASS])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->paginate($limit);
        }
        $this->assign('title', 'Reviews');
        $this->assign('nav', 'appForm');
        $this->assign('subnav', 'reviews');
        $this->assign('params', $params);
        $this->assign('seals', $seals);
        $this->assign('statuses', $sealAppFormStatues);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function dc4()
    {

        $sealAppFormStatues = $this->configModel->getSealAppFormStatues();
        $seals = $this->sealModel->scope('company', $this->company->id)->select();
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $where = [];
        $params = $this->request->param();
        if ($params['sealId']) {
            $where['AppForm.seal_id'] = $params['sealId'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['AppForm.create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        if ($params['name']) {
            $where['AppForm.name'] = ['like', '%' . $params['name'] . '%'];
        }
        $where['AppForm.status'] = AppForm::$STATUS_FINISHED;
        $uid = $this->getCurrentUser();
        // $list = $this->model
        // ->hasWhere('processFlows', ['uid' => $uid, 'status' => ApprovalProcess::$AUDIT_PASS])
        // ->with($this->with ? $this->with : null)
        // ->where($where)
        // ->where('AppForm.company_id', $this->company->id)
        // ->order($sort, $order)
        // ->paginate($limit);

        if (session('is_admin') == 3) {
            $list = $this->model
                ->hasWhere('processFlows', ['uid' => $uid, 'status' => ApprovalProcess::$AUDIT_PASS])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->select();
        } elseif (session('is_admin') == 2) {
            //企业管理员
            $list = $this->model
                ->hasWhere('processFlows', ['status' => ApprovalProcess::$AUDIT_PASS])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->select();
        } else {
            $list = $this->model
                ->hasWhere('processFlows', ['status' => ApprovalProcess::$AUDIT_PASS])
                ->with($this->with ? $this->with : null)
                ->where($where)
                ->where('AppForm.company_id', $this->company->id)
                ->order($sort, $order)
                ->select();
        }

        vendor('phpexcel/PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //exit($objPHPExcel);
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '印章名称')
            ->setCellValue('B1', '文件编号')
            ->setCellValue('C1', '审批类型')
            ->setCellValue('D1', '文件名称')
            ->setCellValue('E1', '申请用章次数')
            ->setCellValue('F1', '状态')
            ->setCellValue('G1', '备注')
            ->setCellValue('H1', '用章时间')
            ->setCellValue('I1', '申请时间');

        //设置A列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);


        for ($i = 0; $i < count($list); $i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['seal']['name']);                                                      //印章名称
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['seal']['number']);                                                    //文件编号
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['approval']['type_name']);                                             //审批类型
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $list[$i]['name']);                                                              //文件名称
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['frequency']);                                                         //申请用章次数
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $list[$i]['status_txt']);                                                        //状态
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $list[$i]['remark']);                                                            //备注
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['chapter_time'] ? date('Y-m-d H:i:s', $list[$i]['chapter_time']) : '');//用章时间
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['create_time']);                                                       //申请时间
        }
        //7.设置保存的Excel表格名称
        $filename = 'yz' . date('ymd', time()) . '.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('cs');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="' . $filename . '"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;


    }

    /**
     * 我的审核-归档审核
     */
    public function reviewArchiveListDetail($id)
    {
        $currentUser = $this->getCurrentUser();
        $row = $this->model
            //->hasWhere('processFlows', ['uid' => $currentUser])
            //->with(['processFlows.user', 'user', 'seal', 'approval'])
            //->where('company_id', $this->company->id)
            ->find($id);
        if (!$row) {
            $this->error('没有找到相关申请表');
        }
        $value = processToGroupSort($row->processFlows);
        $this->assign("processFlowGroups", $value);
        $this->assign("user", $row->user);
        $this->assign("seal", $row->seal);
        $this->assign("approval", $row->approval);
        $this->assign("attachments", []);
        if ($row->imgs) {
            $value1 = (new Picture())->getByIdIn(explode(",", $row->imgs));
            $this->assign("attachments", $value1);
        }
        if ($row->used_img) {
            $value2 = (new Picture())->getByIdIn(explode(",", $row->used_img));
            $this->assign("attachments2", $value2);
        }
        if ($row->used_times > $row->frequency) {
            $row->used_times = $row->frequency;
        }
        if ($row->remaining_times < 0) {
            $row->remaining_times = 0;
        }
        //print_r($value);die;
        //文档
        if ($row->file) {
            $file1 = $row->file;
            $file = explode(',', $file1);
            foreach ($file as $k => $v) {
                $bb[$k]['src'] = "https://" . $_SERVER['HTTP_HOST'] . db('picture')->where(['id' => $v])->value('path');
                $bb[$k]['name'] = db('picture')->where(['id' => $v])->value('name');
            }
            $wd = $bb;
        } else {
            $wd = '';
        }
        $this->assign("wd", $wd);


        $ll = db('approval_process')->where(['form_id' => $id])->order('id asc')->select();
        //print_r($ll);die;
        foreach ($ll as $k => $v) {
            if ($v['remark'] == 1) {
                $reason = '文件名称不全，请核实';
            } elseif ($v['remark'] == 2) {
                $reason = '用章次数可能过多，请重新设置';
            } elseif ($v['remark'] == 3) {
                $reason = '图片上传不够清晰，请核对后重新上传';
            } elseif ($v['remark'] == 4) {
                $reason = $v['remark_content'];
            }

        }
        $this->assign("reason", $reason);
        $this->assign("row", $row);
        return $this->fetch();
    }
}